<template>
  <div style="background-color: #f5f6f7; width: 958px">
    <input-com :headTitle="getInputHint()" :inputValue="this.editForm.description"
                :headSonTitle="''" :maxlength="'300'" @getInputVal="getInputContentVal"></input-com>
    <el-row class="index-edit-desc-btnFrame">
      <el-button class="index-edit-desc-btn" @click="submitForm('editForm')" type="primary">保存</el-button>
    </el-row>
  </div>
</template>
<script>
  import {enums} from '../../../../utils/settings';
  import inputCom from 'components/input/baseTitleInput.vue';
  export default {
    components: {
      inputCom
    },
    props: ['indexType'],
    data() {
      return {
        initForm: [],
        editForm: []
      };
    },
    created() {
      this.initData();
    },
    methods: {
      // 根据指数类型获取指数说明
      initData() {
        let that = this;
        this.ajax({
          url: '/malisk/server/macro/queryMacroIndexDef/' + this.indexType,
          type: 'GET',
          success: (res) => {
            setTimeout(() => {
              this.$pcNProgress.done();
            }, 300);
            that.editForm = res.result;
            that.initForm = JSON.stringify(that.editForm);
          }
        });
      },
      // 获取输入框的提示语
      getInputHint() {
        let inputHint;
        if (parseInt(this.indexType) === enums.SK_INDEX_TYPE.JKL) {
          inputHint = '请输入“当月进口量”的说明文字';
        } else if (parseInt(this.indexType) === enums.SK_INDEX_TYPE.XQZS) {
          inputHint = '请输入“现期指数”的说明文字';
        } else if (parseInt(this.indexType) === enums.SK_INDEX_TYPE.NHYZS) {
          inputHint = '请输入“年化月增速”的说明文字';
        } else if (parseInt(this.indexType) === enums.SK_INDEX_TYPE.LTSD) {
          inputHint = '请输入“流通速度指数”的说明文字';
        }
        return inputHint;
      },
      // 获取说明文字的输入内容
      getInputContentVal(val) {
        this.editForm.description = val;
      },
      // 请求后端接口
      submitForm() {
        // 内容检查：判断提交前后有无内容变化
        if (this.initForm === JSON.stringify(this.editForm)) {
          this.errorTip('内容没有任何变化，请确认');
          return;
        }
        var that = this;
        this.ajax({
          type: 'POST',
          url: '/malisk/server/macro/saveMacroIndexDef',
          data: {
            id: that.editForm.id,
            version: that.editForm.version,
            indexType: that.indexType,
            description: that.editForm.description
          },
          loading: true,
          success: (res) => {
            setTimeout(() => {
              this.$pcNProgress.done();
            }, 300);
            if (res.code === 200) {
              this.successTip('保存成功', 2000);
              that.$emit('sure');
            } else {
              this.errorTip(res.message);
            }
          }
        });
      }
    }
  };
</script>
<style>
  .index-edit-desc-btnFrame {
    width: 100%;
    margin-top: 40px;
    margin-bottom: 20px;
    text-align: center;
  }
  .index-edit-desc-btn {
    width: 140px;
    height: 40px;
  }
</style>
